package com.woshua.tabmodel;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.woshua.tabmodel.fragment.TabFragment;
import com.woshua.tabmodel.indicate.ViewPageIndicate;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private ViewPager vpContent;

    private ArrayList<String> mTitles = new ArrayList<>(Arrays.asList("新闻", "娱乐", "视频"));
    private ViewPageIndicate vpIndicate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vpContent = findViewById(R.id.vpContent);
        vpIndicate = findViewById(R.id.vpIndicate);
        vpIndicate.setVisibleCount(3);
        vpIndicate.addTabs(mTitles);

        vpContent.setAdapter(new TabAdapter(getSupportFragmentManager()));

        vpIndicate.setUpWidthViewPager(vpContent);
    }

    private class TabAdapter extends FragmentPagerAdapter {

        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TabFragment.newInstance(mTitles.get(position));
        }

        @Override
        public int getCount() {
            return mTitles.size();
        }
    }
}
